package com.g.places.api.util;

import org.apache.commons.lang3.StringUtils;

import com.g.places.api.base.PlacesClient.SimpleApiAccess;
import com.g.places.api.execptions.GPlaceAPIKeyException;
import com.g.places.api.execptions.RequestParamGPlaceException;

/**
 * 
 *   general methods to determine the validity of url 
 *   search string , request parameter , API Key  
 * 
 * @author ahmad
 * 
 */
public class URLValidatorUtil{
 

	
	/**
	 * 
	 * @param parameter represent request parameter 
	 * @return true when given parameter is valid otherwise false
	 * @throw RequestParamGPlaceException when parameter is null or required Parameter field is null
	 */
	public static synchronized Boolean validParameter(Parameter parameter ) {
		
		if (parameter == null ) 
				throw new RequestParamGPlaceException(" null param exception");
		
		if (parameter.getName() == null || parameter.getValue() == null ) 
			throw new RequestParamGPlaceException(" invalid parameter exception");
		
		// add valid conditions .... 
		
		
		return true;
		
	}
	
	
	
	/**
	 * @param searchStr
	 * @return
	 */
	public  static synchronized String validSearchStr(String searchStr) {
		
		// valid search 
		
		return searchStr.replaceAll(" ", "+");
	}



	/**
	 * check if API Key is valid   
	 * @param apiKey SimpleApiAccess object represent Google Places API Access  (simple API Access)  
	 * @return apiKey represent Google API Key 
	 */
	public static synchronized String getAPIKey(SimpleApiAccess apiKey) {
		
		if (apiKey == null || StringUtils.isBlank(apiKey.getgAPIKey()) ) 
			 throw new GPlaceAPIKeyException("GPlace Api key exception");
		
		
		return apiKey.getgAPIKey();
	}
	
}
